#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define pb push_back
#define mp make_pair
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define ren(i, a, b) for (int i = a; i >= b; i--)
#define ff first
#define ss second
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d", &n)
#define gll(n) scanf("%lld", &n)
#define gstr(n) scanf("%s", n)
#define gl(n) cin >> n
#define oi(n) printf("%d", n)
#define oll(n) printf("%lld", n)
#define ostr(n) printf("%s", n)
#define ol(n) cout << n
#define os cout << " "
#define on cout << "\n"
#define o2(a, b) cout << a << " " << b
#define all(n) n.begin(), n.end()
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define tr(container, it)                                                      \
  for (__typeof(container.begin()) it = container.begin();                     \
       it != container.end(); it++)
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef vector<vector<ll>> mat;

bool cmp(pii a, pii b) {
  if (a.ss != b.ss)
    return a.ss < b.ss;
  return a.ff < b.ff;
}

int main() {
  ios_base::sync_with_stdio(false);
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  int t, t1 = 0;
  gl(t);
  while (t--) {
    t1++;
    ol("Case #");
    ol(t1);
    ol(": ");
    int n, p;
    cin >> n >> p;
    map<int, int> m[55];
    long double lo = 0.9, hi = 1.1;
    int r[55];
    rep(i, 0, n - 1) cin >> r[i];
    vector<pii> v[55];
    rep(i, 0, n - 1) {
      rep(j, 0, p - 1) {
        int x;
        cin >> x;
        long double y = x;
        y /= r[i];
        long double ri = y / lo, le = y / hi;
        // ol(y);on;
        long double eps = 1e-9;
        int lli = -1, rri = -1;
        rep(ii, max(1, (int)le), ceil(ri)) {
          long double l = lo * ii, h = hi * ii;
          if (y + eps >= l && y - eps <= h) {
            rri = ii;
            if (lli == -1)
              lli = ii;
          }
        }
        // ol(i);os;o2(lli,rri);on;
        if (lli != -1)
          v[i].pb(mp(lli, rri));
      }
    }
    rep(i, 0, n - 1) sort(all(v[i]), cmp);
    int ans = 0;
    int ptr[55] = {0};
    rep(ii, 1, 1000004) {
      rep(i, 0, n - 1) {
        while (1) {
          if (ptr[i] == v[i].size())
            break;
          if (v[i][ptr[i]].ss < ii)
            ptr[i]++;
          else
            break;
        }
      }

      while (1) {
        int cool = 0;
        rep(i, 0, n - 1) {
          if (ptr[i] == v[i].size())
            break;
          if (v[i][ptr[i]].ff <= ii && v[i][ptr[i]].ss >= ii)
            cool++;
          else
            break;
        }
        if (cool != n)
          break;
        else {
          ans++;
          rep(i, 0, n - 1) ptr[i]++;
        }
      }
    }

    ol(ans);
    on;
  }
  return 0;
}
